package com.china08.yxyapi.api.manage.tec.teaching.continuedu.teatrainlesson;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLessonItem;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLessonsVideo;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserFavorite;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTaskLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonItemRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonsVideoRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserFavoriteRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskLessonItemRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.YxyKeChengUtil;

/**
 * 
 * @annotation: 继续教育-师训课程-课程详情
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyTeaTrainLessonDetailController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduLessonItemRepository yxyContinueduLessonItemRepository;

    @Autowired
    private YxyContinueduLessonRepository yxyContinueduLessonRepository;

    @Autowired
    private YxyContinueduUserFavoriteRepository yxyContinueduUserFavoriteRepository;

    @Autowired
    private YxyContinueduUserTaskLessonItemRepository yxyContinueduUserTaskLessonItemRepository;

    @Autowired
    private YxyKeChengUtil yxyKeChengUtil;

    @Autowired
    private YxyContinueduLessonsVideoRepository yxyContinueduLessonsVideoRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/teatrainlesson/detail/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        Integer taskStatus = reqModel.taskStatus;
        String domainName = getDomainName(request);
        RespModel model = new RespModel();
        // 查询课程基本信息
        YxyContinueduLesson yLesson = yxyContinueduLessonRepository.findById(reqModel.lessonId);
        yLesson.setPv(yLesson.getPv() + 1);
        yxyContinueduLessonRepository.save(yLesson);
        List<ContinueduLessonItem> cItems = new ArrayList<YxyTeaTrainLessonDetailController.ContinueduLessonItem>();
        // 查询课时
        if (taskStatus == 0) {
            List<YxyContinueduLessonItem> yLessonItems = yxyContinueduLessonItemRepository
                    .findByLessonIdOrderByOrderNumAsc(reqModel.lessonId);
            if (yLessonItems != null && yLessonItems.size() > 0) {
                for (YxyContinueduLessonItem yxyContinueduLessonItem : yLessonItems) {
                    ContinueduLessonItem cItem = new ContinueduLessonItem();
                    cItem.lessonId = yxyContinueduLessonItem.getLessonId();
                    cItem.orderNum = yxyContinueduLessonItem.getOrderNum();
                    // 获取播放路径
                    YxyContinueduLessonsVideo lessonsVideo = yxyContinueduLessonsVideoRepository
                            .findById(yxyContinueduLessonItem.getVideoId());
                    String md5 = lessonsVideo.getMd5();
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    String url = "";
                    if (domainName.contains("hrbedu")) {
                        url = "https://yxyres.hrbeduy.com/converted/mp4/";
                    } else if (domainName.contains("91yunxiao")) {
                        url = "https://yxy.res.91yunxiao.com/converted/mp4/";
                    } else {
                        url = "https://yxy.res.91yunxiao.com/converted/mp4/";
                    }
                    cItem.playUrl = url + md5.substring(0, 3) + "/" + md5.substring(3, 6) + "/" + md5
                            + ".mp4?&continueEdu_sign=" + uuid;
                    cItem.title = yxyContinueduLessonItem.getTitle();
                    cItem.videoId = yxyContinueduLessonItem.getVideoId();
                    cItem.itemId = yxyContinueduLessonItem.getId();
                    cItem.progress = 0l;
                    cItem.status = 1;
                    cItem.videoSta = lessonsVideo.getStatus();
                    cItems.add(cItem);
                }
            }
        } else {
            List<YxyContinueduUserTaskLessonItem> userTaskLessonItems = yxyContinueduUserTaskLessonItemRepository
                    .findByUserIdAndTaskIdAndLessonIdOrderByOrderNumAsc(userId, reqModel.taskId, reqModel.lessonId);
            if (userTaskLessonItems != null && userTaskLessonItems.size() > 0) {
                for (YxyContinueduUserTaskLessonItem userTaskLessonItem : userTaskLessonItems) {
                    ContinueduLessonItem cItem = new ContinueduLessonItem();
                    cItem.lessonId = userTaskLessonItem.getLessonId();
                    cItem.orderNum = userTaskLessonItem.getOrderNum();

                    // 获取播放路径
                    YxyContinueduLessonsVideo lessonsVideo = yxyContinueduLessonsVideoRepository
                            .findById(userTaskLessonItem.getVideoId());
                    String md5 = lessonsVideo.getMd5();
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    String url = "";
                    if (domainName.contains("hrbedu")) {
                        url = "https://yxyres.hrbeduy.com/converted/mp4/";
                    } else if (domainName.contains("91yunxiao")) {
                        url = "https://yxy.res.91yunxiao.com/converted/mp4/";
                    } else {
                        url = "https://yxy.res.91yunxiao.com/converted/mp4/";
                    }
                    cItem.playUrl = url + md5.substring(0, 3) + "/" + md5.substring(3, 6) + "/" + md5
                            + ".mp4?&continueEdu_sign=" + uuid;

                    cItem.title = userTaskLessonItem.getTitle();
                    cItem.videoId = userTaskLessonItem.getVideoId();
                    cItem.itemId = userTaskLessonItem.getId();
                    cItem.progress = userTaskLessonItem.getCurrentView();
                    cItem.status = userTaskLessonItem.getStatus();
                    cItem.videoSta = lessonsVideo.getStatus();
                    cItems.add(cItem);
                }
            }
        }

        // 判断是否收藏过
        String id = userId + "_" + reqModel.lessonId;
        YxyContinueduUserFavorite favorite = yxyContinueduUserFavoriteRepository.findById(id);
        if (favorite == null) {
            model.isHave = 0;
        } else {
            model.isHave = 1;
        }

        if (yLesson != null) {
            model.lessonId = yLesson.getId();
            model.count = yLesson.getCount();
            model.cover = yLesson.getCover();
            model.duration = yLesson.getDuration();
            model.name = yLesson.getName();
            model.pv = yLesson.getPv();
            model.summary = yLesson.getSummary();
            model.vv = yLesson.getVv();
            model.updateTime = DateToolUtils.format(yLesson.getUpDate(), DateToolUtils.FORMAT_SHORT_YMD);
        }

        model.yLessonItems = cItems;
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 课程ID
        public String lessonId;
        // 任务状态 0查询课程-课时表的信息 1-查询我的课程-课时表信息
        public Integer taskStatus;
        // 任务ID
        public String taskId;
    }

    public class ContinueduLessonItem {

        public String lessonId;

        public String title;

        public String videoId;

        public Integer orderNum = 0;

        public String playUrl;

        public String itemId;

        public Long progress;

        public String cover;

        public Integer status;

        public Integer videoSta;
    }

    protected static class RespModel {

        public String lessonId;// 课程Id

        public String name;// 课程名称

        public String summary;// 简介

        public String cover;// 封面

        public Integer count;// 总课时

        public Integer duration;// 总时长

        public Integer isHave; // 是否收藏过 0 没有 1 有

        public Integer pv;// 观看次数

        public Integer vv;// 观看人数

        public String updateTime; // 更新时间

        public List<ContinueduLessonItem> yLessonItems;
    }
}
